using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Banshee.Base.Winforms.Widgets
{
    public class StreamPositionLabel:Label
    {

        private double buffering_progress;
        private bool is_buffering;
        private bool is_contacting;
        private SeekSlider seekRange;

        public StreamPositionLabel(SeekSlider seekRange)
        {
            SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true);
            AutoSize = false;
            TextAlign = System.Drawing.ContentAlignment.BottomCenter;
            this.seekRange = seekRange;
            this.seekRange.ValueChanged += new EventHandler(seekRange_ValueChanged);
            this.seekRange.DurationChanged += new EventHandler(seekRange_DurationChanged);
            UpdateLabel();
        }

        void seekRange_DurationChanged(object sender, EventArgs e)
        {
            UpdateLabel();
            
        }

        void seekRange_ValueChanged(object sender, EventArgs e)
        {
            UpdateLabel();
        }
        public double BufferingProgress
        {
            get { return buffering_progress; }
            set
            {
                buffering_progress = Math.Max(0.0, Math.Min(1.0, value));
                UpdateLabel();
            }
        }
        private void UpdateLabel()
        {
            if (is_buffering)
            {
                double progress = buffering_progress * 100.0;
                UpdateLabel("Buffering" + ": " + progress.ToString("0.0") + "%");
            }
            else if (is_contacting)
            {
                UpdateLabel("Contacting...");
            }
            else if (seekRange.Value == 0 && seekRange.Duration == 0)
            {
                UpdateLabel("Idle");
            }
            else if (seekRange.Value == seekRange.Duration)
            {
                UpdateLabel(FormatDuration((long)seekRange.Value));
            }
            else
            {
                UpdateLabel(String.Format("{0} of {1}",
                    FormatDuration((long)seekRange.Value), FormatDuration((long)seekRange.Maximum)));
            }
            Console.WriteLine("Updated label");
        }
        private void UpdateLabel(string text)
        {
            if (this.InvokeRequired)
            {
                BeginInvoke((MethodInvoker)delegate()
                {
                    this.Text = text;
                });
            }else
            this.Text = text;
           
        }
        private static string FormatDuration(long time)
        {
            return (time > 3600 ?
                    String.Format("{0}:{1:00}:{2:00}", time / 3600, (time / 60) % 60, time % 60) :
                    String.Format("{0}:{1:00}", time / 60, time % 60));
        }
        public bool IsBuffering
        {
            get { return is_buffering; }
            set
            {
                if (is_buffering != value)
                {
                    is_buffering = value;
                    UpdateLabel();
                }
            }
        }

        public bool IsContacting
        {
            get { return is_contacting; }
            set
            {
                if (is_contacting != value)
                {
                    is_contacting = value;
                    UpdateLabel();
                }
            }
        }
        private void OnSliderUpdated(object o, EventArgs args)
        {
            UpdateLabel();
        }
    }
}
